package com.xuemeng.web;


import com.github.pagehelper.PageHelper;
import com.xuemeng.entity.Tgroup;
import com.xuemeng.model.JqgridBean;
import com.xuemeng.model.PageRusult;
import com.xuemeng.service.TgroupService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;
import javax.annotation.Resource;
import java.util.*;


/**
 * @author xuemeng
 * @date 2018/12/12 22:03
 * 客户分组操作类
 */
@Controller
@RequestMapping("/group")
public class GroupController {

        @Resource
        private TgroupService tgroupService;

        /**
         * 进入客户分类主页面
         *
         * @return
         */
        @RequestMapping("/toGroupManage")
        @RequiresPermissions(value = {"客户分类"})
        public String toGroupManage() {

                return "content/group";
        }

        /**
         * 分页查询角色信息
         */

        @ResponseBody
        @RequestMapping(value = "/list")
        @RequiresPermissions(value = {"客户分类"})
        public Map<String, Object> list(JqgridBean jqgridbean) throws Exception {
                LinkedHashMap<String, Object> resultmap = new LinkedHashMap<String, Object>();
                LinkedHashMap<String, Object> datamap = new LinkedHashMap<String, Object>();

                Example troleExample = new Example(Tgroup.class);
                Example.Criteria criteria = troleExample.or();

                if (StringUtils.isNotEmpty(jqgridbean.getSearchField())) {
                        if ("name".equalsIgnoreCase(jqgridbean.getSearchField())) {
                                        criteria.andLike("name", "%"+jqgridbean.getSearchString()+"%");
                        }
                }

                PageHelper.startPage(jqgridbean.getPage(), jqgridbean.getLength());
                List<Tgroup> groupList = tgroupService.selectByExample(troleExample);
                PageRusult<Tgroup> pageRusult = new PageRusult<Tgroup>(groupList);

                resultmap.put("currpage", String.valueOf(pageRusult.getPageNum()));
                resultmap.put("totalpages", String.valueOf(pageRusult.getPages()));
                resultmap.put("totalrecords", String.valueOf(pageRusult.getTotal()));
                resultmap.put("datamap", groupList);

                return resultmap;
        }


        @ResponseBody
        @RequestMapping(value = "/addUpdateGroup")
        @RequiresPermissions(value = {"客户分类"})
        public Map<String, Object> addUpdateGroup(Tgroup group) {
                LinkedHashMap<String, Object> resultmap = new LinkedHashMap<String, Object>();
                try {
                        if (group.getId() == null) {//新建
                                //首先判断用户名是否可用
                                Example troleExample = new Example(Tgroup.class);
                                troleExample.or().andEqualTo("name", group.getName());
                                List<Tgroup> rolelist = tgroupService.selectByExample(troleExample);
                                if (rolelist != null && rolelist.size() > 0) {
                                        resultmap.put("state", "fail");
                                        resultmap.put("mesg", "当前角色名已存在");
                                        return resultmap;
                                }
                                tgroupService.saveNotNull(group);
                        } else {//编辑
                                Tgroup oldObject = tgroupService.selectByKey(group.getId());
                                if (oldObject == null) {
                                        resultmap.put("state", "fail");
                                        resultmap.put("mesg", "当前角色名不存在");
                                        return resultmap;
                                } else {
                                        tgroupService.updateNotNull(group);
                                }
                        }
                        resultmap.put("state", "success");
                        resultmap.put("mesg", "操作成功");
                        return resultmap;
                } catch (Exception e) {
                        e.printStackTrace();
                        resultmap.put("state", "fail");
                        resultmap.put("mesg", "操作失败，系统异常");
                        return resultmap;
                }
        }


        @ResponseBody
        @RequestMapping(value = "/deleteGroup")
        @RequiresPermissions(value = {"客户分类"})
        public Map<String, Object> deleteuser(Tgroup group) {
                LinkedHashMap<String, Object> resultmap = new LinkedHashMap<String, Object>();
                try {
                        if (group.getId() != null && !group.getId().equals(0)) {
                                Tgroup role = tgroupService.selectByKey(group.getId());
                                if (role == null) {
                                        resultmap.put("state", "fail");
                                        resultmap.put("mesg", "删除失败,无法找到该记录");
                                        return resultmap;
                                } else {

                                        tgroupService.delete(group.getId());
                                }
                        } else {
                                resultmap.put("state", "fail");
                                resultmap.put("mesg", "删除失败");
                        }


                        resultmap.put("state", "success");
                        resultmap.put("mesg", "删除成功");
                        return resultmap;
                } catch (Exception e) {
                        e.printStackTrace();
                        resultmap.put("state", "fail");
                        resultmap.put("mesg", "删除失败，系统异常");
                        return resultmap;
                }
        }


        @ResponseBody
        @RequestMapping(value = "/selectGroupById")
        @RequiresPermissions(value = {"客户分类"})
        public Map<String, Object> selectRoleById(Tgroup group) {
                LinkedHashMap<String, Object> resultmap = new LinkedHashMap<String, Object>();
                try {
                        if (group.getId() != null && !group.getId().equals(0)) {
                                group = tgroupService.selectByKey(group.getId());
                                if (group == null) {
                                        resultmap.put("state", "fail");
                                        resultmap.put("mesg", "无法找到该记录");
                                        return resultmap;
                                }
                        } else {
                                resultmap.put("state", "fail");
                                resultmap.put("mesg", "无法找到该记录的id");
                                return resultmap;
                        }

                        resultmap.put("group", group);
                        resultmap.put("state", "success");
                        resultmap.put("mesg", "获取成功");
                        return resultmap;
                } catch (Exception e) {
                        e.printStackTrace();
                        resultmap.put("state", "fail");
                        resultmap.put("mesg", "获取失败，系统异常");
                        return resultmap;
                }
        }





}
